//
//  HomePageViewController.m
//  OXFAM_iOS
//
//  Created by Karl on 2017/3/24.
//  Copyright © 2017年 i-Craftsmen ltd. All rights reserved.
//

#import "HomePageViewController.h"
#import "NativeViewManager.h"
#import "HomepagePopView.h"
#import "ButtomLogoView.h"
#import "ManuView.h"
#import "ToturialView.h"

#import "UIView+PopAnimation.h"

#import <AVFoundation/AVFoundation.h>

@interface HomePageViewController ()<ManuViewDelegate>{
    UIImageView *_viewBg;
    UIImageView *_topImg;
    ButtomLogoView *_butView;
    
    UIButton *_captureBtn;
    UILabel *_captureLab;
    UIButton *_scanBtn;
    UILabel *_scanLab;
    
    ManuView *_manuView;
    HomepagePopView *_popView;
}
@property (nonatomic,strong) ToturialView *toturialView;
@end

@implementation HomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:142.0/255.0 green:176.0/255.0 blue:198.0/255.0 alpha:1.0];

    NSLog(@"%@",[LocalizeManager sharedManager].isCN?@"isCN":@"notCN");

    [self initUI];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    if ([NativeViewManager sharedManager].showHomePop) {
        [_popView popAnimationShow:YES];
        
        [NativeViewManager sharedManager].showHomePop = NO;
    }
}

- (void)initUI{
    
    _viewBg = [UIImageView new];
    [_viewBg setImage:kImgFromFile(Babel(@"home_bg"), @"png")];
    [_viewBg setContentMode:UIViewContentModeScaleAspectFit];
    [self.view addSubview:_viewBg];
    
    _topImg = [UIImageView new];
    [_topImg setImage:kImgFromFile(Babel(@"home_top"), @"png")];
    [self.view addSubview:_topImg];
    
    _scanBtn = [UIButton new];
    [_scanBtn setBackgroundImage:kImgFromFile(Babel(@"home_scan"), @"png") forState:UIControlStateNormal];
    [_scanBtn addTarget:self action:@selector(onScanPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_scanBtn];
    
    _scanLab = [UILabel new];
    [_scanLab setText:Babel(@"home_scan_text")];
    [_scanLab setNumberOfLines:0];
    if ([LocalizeManager sharedManager].isCN) {
        [_scanLab setFont:[UIFont systemFontOfSize:17.0]];
    }else{
        [_scanLab setFont:[UIFont systemFontOfSize:14.0]];
    }
//    [_scanLab setMinimumFontSize:14.0];
    [_scanLab setTextAlignment:NSTextAlignmentCenter];
    [_scanLab setTextColor:[UIColor whiteColor]];
    [self.view addSubview:_scanLab];
    [_scanLab sizeToFit];
    
    _captureBtn = [UIButton new];
    [_captureBtn setBackgroundImage:kImgFromFile(Babel(@"home_capture"), @"png") forState:UIControlStateNormal];
    [_captureBtn addTarget:self action:@selector(onCapturePressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_captureBtn];
    
    _captureLab = [UILabel new];
    [_captureLab setNumberOfLines:0];
    [_captureLab setText:Babel(@"home_capture_text")];
    
    if ([LocalizeManager sharedManager].isCN) {
        [_captureLab setFont:[UIFont systemFontOfSize:17.0]];
    }else{
        [_captureLab setFont:[UIFont systemFontOfSize:14.0]];
    }
//    [_captureLab setMinimumFontSize:14.0];
    [_captureLab setTextAlignment:NSTextAlignmentCenter];
    [_captureLab setTextColor:[UIColor whiteColor]];
    [self.view addSubview:_captureLab];
    [_captureLab sizeToFit];
    
    _butView = [[ButtomLogoView alloc] init];
    [self.view addSubview:_butView];
    
    _manuView = [[ManuView alloc] init];
    _manuView.delegate = self;
    [self.view addSubview:_manuView];
    
    _popView = [[HomepagePopView alloc] init];
    [_popView setFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    _popView.hidden = YES;
    [self.view addSubview:_popView];
    
   

}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
    CGFloat scale = 1.0;
    if (isPad) {
        scale = SizeScaleX;
    }else{
        scale = SizeScaleY;
    }
    
    
    _viewBg.left = 0;
    _viewBg.top = 0;
    _viewBg.width = self.view.width;
    _viewBg.height =  self.view.height;
    
    _topImg.width = 250.7 * scale;
    _topImg.height = 117.7  * scale;
    _topImg.top = 0;
    _topImg.centerX = self.view.centerX;
    
    
    _captureBtn.width = 188  * scale;
    _captureBtn.height = 188  * scale;
    _captureBtn.right = self.view.width/2 - 37  * scale;
    _captureBtn.top = (self.view.height - _scanBtn.height)/2;
    
    _captureLab.centerX = _captureBtn.centerX;
    _captureLab.top = _captureBtn.bottom + 6.7;
//    _captureLab.width = _captureBtn.width;
//    _captureLab.height = 30;
    
    _scanBtn.width = 188  * scale;
    _scanBtn.height = 188  * scale;
    _scanBtn.left = self.view.width/2 + 37  * scale;
    _scanBtn.top = (self.view.height - _scanBtn.height)/2;
    
    _scanLab.centerX = _scanBtn.centerX;
    _scanLab.top = _scanBtn.bottom + 6.7;
//    _scanLab.width = _scanBtn.width;
//    _scanLab.height = 30;
    
    _butView.width = self.view.width;
    _butView.height = 66  * scale;
    _butView.left = 0;
    _butView.bottom  = self.view.bottom;
    
    _manuView.width = 75  * scale;
    _manuView.height = 75  * scale + 50  * scale * 4;
    _manuView.top = 12  * scale;
    _manuView.right = self.view.right - 34.3 * scale;
    
    _popView.top = 0;
    _popView.left = 0;
    _popView.width = self.view.width;
    _popView.height = self.view.height - _butView.height;
    
    _toturialView.left = 0;
    _toturialView.top = 0;
    _toturialView.width = self.view.width;
    _toturialView.height = self.view.height;
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (void)onScanPressed:(id)sender{
    NSLog(@"scan pressed");

    if ([self checkCameraPrivacy]) {
        UnitySendMessage("_sceneManager", "RouteScene", "scan");
        [[NativeViewManager sharedManager] view:ViewTypeScanpage show:YES];
        
    }else{
        if (IOS_VERSION_8_OR_LATER) {
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
            
        }else{
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Privacy&path=CAMERA"]];
        }
    }
}

- (void)onCapturePressed:(id)sender{
    NSLog(@"capture pressed");
    if ([self checkCameraPrivacy]) {
        UnitySendMessage("_sceneManager", "RouteScene", "capture");
        [[NativeViewManager sharedManager] view:ViewTypeCapturepage show:YES];
    }else{
        if (IOS_VERSION_8_OR_LATER) {
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
        }else{
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Privacy&path=CAMERA"]];
        }

    }
    
}

- (void)manuViewAction:(NSString *)action{
    NSLog(@"manu action %@ need to process",action);
    if ([action isEqualToString:Babel(@"manu_tutorial")]) {
//        self.toturialView.hidden = NO;
        [self.toturialView pop];
    }
}

- (ToturialView *)toturialView{
    if (!_toturialView) {
        _toturialView = [[ToturialView alloc] init];
        [self.view addSubview:_toturialView];
    }
    return _toturialView;
}

#pragma mark - Private
- (BOOL)checkCameraPrivacy{
    BOOL allow = NO;
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    switch (status) {
        case AVAuthorizationStatusNotDetermined:
            NSLog(@"AVAuthorizationStatusNotDetermined");
//            [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
//                
//                if (granted) {
//                    //第一次用户接受
//                }else{
//                    //用户拒绝
//                }
//            }];
            allow = YES;
            break;
        case AVAuthorizationStatusAuthorized:
            NSLog(@"AVAuthorizationStatusAuthorized");
            allow = YES;
            break;
        case AVAuthorizationStatusDenied:
            NSLog(@"AVAuthorizationStatusDenied");
            break;
        case AVAuthorizationStatusRestricted:
            NSLog(@"AVAuthorizationStatusRestricted");
//            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"oxfamar:root=Privacy&path=CAMERA"]];

            break;
        default:
            break;
    }
    return allow;
}

@end
